package com.xk857.simpleeffect;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.entity.About;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import org.sufficientlysecure.htmltextview.HtmlTextView;

import java.lang.reflect.Type;
import java.util.function.Consumer;

public class AppNoticeActivity extends AppCompatActivity {

    private TitleBar titleBar;
    private HtmlTextView htmlText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.white));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_app_notice);
        initView();
        initData();
    }

    private void initData() {
        // 默认显示隐私协议，传递false显示用户协议
        boolean isPrivate = getIntent().getBooleanExtra("isPrivate", true);
        Type type = new TypeToken<Result<About>>() {}.getType();
        if (isPrivate) {
            titleBar.setTitle("隐私协议");
            OkHttpUtils.getInstance().get(this, RequestEnum.GET_PRIVATE_NOTICE, type, false, (Consumer<About>) about ->
                    runOnUiThread(() -> htmlText.setHtml(about.getContent())));
        } else {
            OkHttpUtils.getInstance().get(this, RequestEnum.GET_USER_NOTICE, type, false, (Consumer<About>) about ->
                    runOnUiThread(() -> htmlText.setHtml(about.getContent())));
        }
    }

    private void initView() {
        titleBar = findViewById(R.id.title_bar);
        titleBar.setLeftClickListener(v -> finish());
        htmlText = findViewById(R.id.html_text);
    }
}